﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
  <head>
    <meta charset="utf-8" />
    <title>SQL Assitant</title>
    <link href="./css/theme.css" rel="stylesheet" />
    <link href="./css/common.css" rel="stylesheet" />
    <link href="./css/demo.css" rel="stylesheet" />
    <link href="./css/scrollBar.css" rel="stylesheet" />
    <script src="./js/jquery.min.js"></script>
    <script src="./js/theme.js"></script>
    <script src="./js/cookie.js"></script>
    <script src="./js/suggestionParser.js"></script>
    <script src="./js/errorcodeParser.js"></script>
    <script src="./js/stringReader.js"></script>
    <script src="./js/scrollBar.js"></script>
    <style type="text/css">
      .keyInfoStyle {
        position: absolute;
        z-index: 9999;
        display: none;
        font-weight: normal;
        font-style: normal;
        box-shadow: var(--el-box-shadow-light);
        border: 1px solid var(--el-border-color-light);
        border-radius: 4px;
        color: rgb(0, 0, 0);
        overflow: auto;
        left: 74px;
        top: 28px;
        width: 260px !important;
      }
      .keyInfoStyle td {
        padding: 2px 7px;
      }
      #keywordInfoBody > tr > td {
        background-color: var(--el-bg-color-overlay);
        color: var(--el-text-color-regular);
        cursor: pointer;
      }
      #keywordInfoBody > tr > td:hover {
        background-color: var(--el-fill-color-light);
        cursor: pointer;
      }
      .keywordselect {
        font-size: 10px;
        line-height: 14px;
        width: 260px;
        height: 19px;
        border: 1px solid var(--el-border-color);
        background: none;
      }
      .showList {
        position: absolute;
        top: 4px;
        left: 313px;
        background: url(img/down.png) no-repeat right bottom;
        width: 20px;
        height: 16px;
        cursor: pointer;
      }
      .hideList {
        position: absolute;
        top: 4px;
        left: 313px;
        background: url(img/up.png) no-repeat right bottom;
        width: 20px;
        height: 16px;
        cursor: pointer;
      }
    </style>
    <script>
      var recordPath = './openGauss1.0.0/en';
      setCookie('DS_recordDataPath', recordPath);
    </script>
    <script>
      var commands;
      var errorcodes;
      var selectedCmdAlias = 'temp';
      var isNotSingle = true;
      var oTextSelection = '';
      var currentSQL = '';
      var backString = '';
      var rightMenu = '';
      var divDefault = '';
      var divGoBack = '';
      var formerCmd = '';
      var curCmd = '';

      function getSQLHtml(tempValues) {
        var tempSuggestion = '<pre>';
        $.each(tempValues, function (n, v) {
          if (v.alias.indexOf('|') > -1) {
            tempSuggestion =
              tempSuggestion +
              "· <a href='#' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);goBack(\"block\");' class='linkText'>" +
              v.id +
              '</a>\r\n';
          } else {
            tempSuggestion =
              tempSuggestion +
              "· <a href='#' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);goBack(\"block\");' class='linkText'>" +
              v.alias +
              '</a>\r\n';
          }
        });
        tempSuggestion = tempSuggestion + '</pre>';
        return tempSuggestion;
      }
      function hideKeyWord(needHide) {
        if (needHide) {
          $('#keywordInfo').hide();
          $('#listStatus').removeClass('hideList').addClass('showList');
        } else {
          $('#keywordInfo').show();
          $('#listStatus').removeClass('showList').addClass('hideList');
        }
      }
      function hideOrShowKeyWord() {
        if ($('#keywordInfo').is(':visible')) {
          hideKeyWord(true);
        } else {
          hideKeyWord(false);
        }
      }
      function hideKeyWordList(str) {
        $('#keyword').val(str);
        hideKeyWord(true);
      }
      function getSQLHtmlForList(tempValues) {
        var tempSuggestion = '';
        $.each(tempValues, function (n, v) {
          if (v.alias.indexOf('|') > -1) {
            tempSuggestion =
              tempSuggestion +
              "<tr><td align='left' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);hideKeyWordList(this.innerText);'>" +
              v.id +
              '</td></tr>';
          } else {
            tempSuggestion =
              tempSuggestion +
              "<tr><td align='left' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);hideKeyWordList(this.innerText);'>" +
              v.alias +
              '</td></tr>';
          }
        });
        $('#keywordInfo').css('height', '300px');
        $('#keywordInfoBody').html(tempSuggestion);
      }
      function checkField(keyword) {
        var tempSuggestion = '';
        var tempValues = commands.cmds;
        var temStr = keyword.toUpperCase();
        var rowCount = 0;
        $.each(tempValues, function (n, v) {
          if (v.alias.indexOf(temStr) == 0) {
            rowCount++;
            if (v.alias.indexOf('|') > -1) {
              tempSuggestion =
                tempSuggestion +
                "<tr><td align='left' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);hideKeyWordList(this.innerText);'>" +
                v.id +
                '</td></tr>';
            } else {
              tempSuggestion =
                tempSuggestion +
                "<tr><td align='left' onclick='selectedCmdAlias=this.innerText;processInput(this.innerText);hideKeyWordList(this.innerText);'>" +
                v.alias +
                '</td></tr>';
            }
          }
        });
        if (rowCount < 16) {
          $('#keywordInfo').css('height', 14 * rowCount + 'px');
        } else {
          $('#keywordInfo').css('height', '300px');
        }
        $('#keywordInfoBody').html(tempSuggestion);
        hideKeyWord(false);
      }
      function processInput(tempId) {
        if ('hideAssistant' == tempId) {
          myCallDBAssistant('hide');
          return;
        }
        if (document.selection) {
          document.selection.empty();
        } else {
          window.getSelection().removeAllRanges();
        }
        if (divGoBack != '') {
          divGoBack.remove ? divGoBack.remove() : divGoBack.removeNode(true);
        }
        if (rightMenu != '') {
          rightMenu.remove ? rightMenu.remove() : rightMenu.removeNode(true);
        }

        tempId = $.trim(tempId);

        hideKeyWord(true);
        if ('' != tempId) {
          var tempIds = tempId.split(/;/);
          tempId = tempIds[tempIds.length - 1];
          if ('' == tempId || '$$' == tempId) tempId = tempIds[tempIds.length - 2];
          var tokens = StringReader(tempId);
          var tempCmds = [];
          var curPos = 0;
          for (var li = 0; li < tokens.length; li++) {
            if (tempCmds.length == 0) {
              tempCmds = commands.find(tokens[li].toUpperCase(), curPos);
              if (tempCmds.length != 0) {
                curPos++;
              }
            } else {
              var formmerA = tempCmds;
              tempCmds = commands.findInArray(tempCmds, tokens[li].toUpperCase(), curPos);
              if (tempCmds.length != 0 && formmerA != tempCmds) {
                curPos++;
              }
            }
          }

          if (tempCmds.length > 0) {
            var tempSuggestion = '';
            if (tempCmds.length == 1) {
              curCmd = tempCmds[0];
              if (curCmd.suggestion != '') {
                if (isNotSingle) myCallDBAssistant('show');
                tempSuggestion =
                  "<h4 class='sectionCmdTitle'>" +
                  curCmd.alias +
                  '</h4>' +
                  curCmd.suggestion +
                  tempSuggestion;
                $('#keyword').val(curCmd.alias);
                if (curCmd.subs.length > 0) {
                  var tempSubSuggestion = '';
                  $(curCmd.subs).each(function (i, sub) {
                    {
                      var tempAllKeywords = '.*?' + sub.id.replace(/\s/g, '.*?');
                      var reg = new RegExp(tempAllKeywords, 'gi');
                      if (reg.test(tempId) && '' != sub.suggestion) {
                        tempSubSuggestion = sub.suggestion + tempSubSuggestion;
                      }
                    }
                  });
                  if (tempSubSuggestion != '')
                    tempSuggestion =
                      '<p class="sectionSugTitle">Suggestion</p>' +
                      tempSubSuggestion +
                      "<hr class='hrStyle'>" +
                      tempSuggestion;
                }
              }
            } else {
              curCmd = commands.getOneFromArray(tempCmds, curPos);

              if (curCmd != '') {
                if (isNotSingle) myCallDBAssistant('show');
                tempSuggestion =
                  "<h4 class='sectionCmdTitle'>" +
                  curCmd.alias +
                  '</h4>' +
                  curCmd.suggestion +
                  tempSuggestion;
                $('#keyword').val(curCmd.alias);
                if (curCmd.subs.length > 0) {
                  var tempSubSuggestion = '';
                  $(curCmd.subs).each(function (i, sub) {
                    {
                      var tempAllKeywords = '.*?' + sub.id.replace(/\s/g, '.*?');
                      var reg = new RegExp(tempAllKeywords, 'gi');
                      if (reg.test(tempId) && '' != sub.suggestion) {
                        tempSubSuggestion = sub.suggestion + tempSubSuggestion;
                      }
                    }
                  });
                  if (tempSubSuggestion != '')
                    tempSuggestion =
                      '<p class="sectionSugTitle">Suggestion</p>' +
                      tempSubSuggestion +
                      "<hr class='hrStyle'>" +
                      tempSuggestion;
                }
              } else {
                backString = tempId;
                if (isNotSingle) myCallDBAssistant('show');
                tempSuggestion = getSQLHtml(tempCmds);
                formerCmd = '';
              }
            }
            if (curCmd != '' && formerCmd == '') {
              $('#divSuggestion').html(tempSuggestion);
              $(document).scrollTop(0);
              assistantHighlight('assistantExamples');
              formerCmd = curCmd;
            } else if (curCmd != '' && formerCmd != '' && curCmd.alias != formerCmd.alias) {
              $('#divSuggestion').html(tempSuggestion);
              $(document).scrollTop(0);
              assistantHighlight('assistantExamples');
              formerCmd = curCmd;
            } else if (formerCmd == '') {
              $('#divSuggestion').html(tempSuggestion);
              $(document).scrollTop(0);
            }
          } else {
            backString = tempId;
            $('#divSuggestion').html(getSQLHtml(commands.cmds));
            $(document).scrollTop(0);
            formerCmd = '';
          }
        } else {
          backString = tempId;
          $('#divSuggestion').html(getSQLHtml(commands.cmds));
          $(document).scrollTop(0);
          formerCmd = '';
        }
      }

      function processErrorCode(errorCode) {
        var err = errorcodes.find($.trim(errorCode));
        if (err != '') {
          var tempHtml =
            '<p class="sectionCmdTitle">' +
            errorCode +
            '</p><p><span class="sectionTitle">Descrition</span></p><p><span class="sectionText">' +
            err.description +
            '</span></p><p><span class="sectionTitle">SQLSTATE</span></p><p><span class="sectionText">' +
            err.sqlstate +
            '</span></p><p><span class="sectionTitle">Possible Causes</span></p><p><span class="sectionText">' +
            err.cause +
            '</span></p><p ><span class="sectionTitle">Solution</span</p><p><span class="sectionText">' +
            err.solution +
            '</span></p>';
          $('#divSuggestion').html(tempHtml);
          if (isNotSingle) myCallDBAssistant('show');
        }
      }

      function processSelection1() {
        if (oTextSelection != '') {
          myCallDBAssistantProcessSelection(oTextSelection);
        }
      }

      function processSelection2() {
        if (oTextSelection != '') {
          myCallDBAssistantProcessSelection2(oTextSelection);
        }
      }

      function displayVersionNote(curVersion) {
        var divVersionNote = $(
          '<div style="display:none;line-height:22px;background-color:#fefce8;position:fixed;top:10px;margin:2 0 0 2;padding-left:6px;padding-right:6px;border-radius:4px;border:1px solid;border-color:#aaa9a9;"></div>',
        );
        var p1 = $(
          '<p style="margin-top:5px;">Please download the corresponding version of knowledge base in FusionInsight LibrA from huawei support website.</p>',
        );
        divVersionNote.append(p1);
        $('body').append(divVersionNote);
      }

      function disableNote(disable) {
        if (disable) {
          if (divDefault != '') {
            divDefault.remove ? divDefault.remove() : divDefault.removeNode(true);
            divDefault = '';
          }
        } else {
          divDefault = $(
            '<div style="line-height:22px;background-color:#fefce8;position:fixed;bottom:0px;margin:10 0 0 2;padding-left:6px;padding-right:6px;border-radius:4px;border:1px solid;border-color:#aaa9a9;">',
          );
          var p1 = $(
            '<p style="margin-bottom:0px;">In the middle window, if you <span style="color:#ed6b44">choose the related SQL or errorcode</span>, the SQL assistant will show the <span style="color:#ed6b44">related information</span>.</p>',
          );
          var p2 = $(
            '<p align="right" style="margin-top:-5px;margin-bottom:5px;color:#016bd8;"><span style="cursor:hand;" onclick="disableNote(true);myCallDBAssistantDisableNote(\'\');">got it</span></p>',
          );
          divDefault.append(p1);
          divDefault.append(p2);
          $('body').append(divDefault);
        }
      }

      function setCurrentSQL(sql) {
        currentSQL = sql;
      }

      function loadSuggestion(docRealPath) {
        commands = new Commands();
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('GET', recordPath + '/suggestion.xml', false);
        xmlhttp.send();
        var xml = xmlhttp.responseXML;

        commands.load(xml);
        getSQLHtmlForList(commands.cmds);
      }

      function loadErrorcode(docRealPath) {
        errorcodes = new ErrorCodes();
        processInput(currentSQL);
        myCallDBAssistant('show');
      }

      function isHidden(oDiv) {
        var vDiv = oDiv.nextSibling;
        while (vDiv.nodeType == 3) {
          vDiv = vDiv.nextSibling;
        }
        vDiv.style.display = vDiv.style.display == 'none' ? 'block' : 'none';
        var pngs = oDiv.getElementsByTagName('img');
        switch (pngs[1].style.display) {
          case 'none':
            pngs[0].style.display = 'none';
            pngs[1].style.display = 'inline';
            break;
          case 'inline':
            pngs[0].style.display = 'inline';
            pngs[1].style.display = 'none';
            break;
        }
      }

      function goBack(display) {
        if (display == 'none') {
          divGoBack.remove ? divGoBack.remove() : divGoBack.removeNode(true);
          divGoBack = '';
          processInput(backString);
        } else {
          divGoBack = $(
            '<div style="line-height:22px;position:fixed;top:8px;left:335px;padding-left:6px;padding-right:1px;border-radius:4px;border:0px solid;border-color:#aaa9a9;cursor:pointer;"></div>',
          );
          var img1 = $(
            '<img src="img/goback.png" border="0" title="Go Back" onClick="javascript:goBack(\'none\');">',
          );
          divGoBack.append(img1);
          $('body').append(divGoBack);
        }
      }
      function myCallDBAssistant() {}
      $(function () {
        $('#keywordInfo').scrollBar({ width: 252 });
        loadSuggestion(recordPath);
        processInput();
        $('body').contextmenu(function (event) {
          if (document.selection) {
            oTextSelection = document.selection.createRange().text;
          } else {
            oTextSelection = window.getSelection().toString();
          }
          var leftX = event.pageX;
          var topY = event.pageY;

          if (oTextSelection != '' && rightMenu == '') {
            var bodyW = $('body').width();
            if (bodyW <= leftX + 155) {
              leftX = bodyW - 155;
            }
            rightMenu = $(
              '<div style="box-shadow:-10px 0 10px rgba(255,255,255,0.01),2px 0 3px gray,0 -10px 10px rgba(255,255,255,0.01),2px 2px 3px gray;width:140px;top:' +
                topY +
                'px;left:' +
                leftX +
                'px;padding:2px 2px 2px 2px;position:absolute;background-image:url(img/assistantCopy.png);border: 1px solid gray;font-size:10px;"></div>',
            );
            var span1 = $(
              '<div style="padding-left:30px;margin:0 0 0 0;border:1px solid #F0F0F0;font-size:12px;line-height:22px;cursor:pointer;" onclick="javascript:processSelection1();" onmouseover="this.style.borderColor=\'#AECFF7\'" onmouseout="this.style.borderColor=\'#F0F0F0\'">Copy</div>',
            );
            var span2 = $(
              '<div style="padding-left:30px;margin:0 0 0 0;border:1px solid #F0F0F0;font-size:12px;line-height:22px;cursor:pointer;" onclick="javascript:processSelection2();" onmouseover="this.style.borderColor=\'#AECFF7\'" onmouseout="this.style.borderColor=\'#F0F0F0\'">Copy to Terminal</div>',
            );
            rightMenu.append(span1);
            rightMenu.append(span2);
            $('body').append(rightMenu);
          } else if (oTextSelection != '' && rightMenu != '') {
            var bodyW = $('body').width();
            if (bodyW <= leftX + 155) {
              leftX = bodyW - 155;
            }
            rightMenu.css('left', leftX);
            rightMenu.css('top', topY);
          } else if (rightMenu != '') {
            rightMenu.remove ? rightMenu.remove() : rightMenu.removeNode(true);
            rightMenu = '';
          }
          return false;
        });
        document.onclick = function () {
          if (rightMenu != '') {
            rightMenu.remove ? rightMenu.remove() : rightMenu.removeNode(true);
            rightMenu = '';
          }
        };
        document.onkeydown = function (e) {
          var e = window.event ? window.event : e;
          if (e.ctrlKey && e.keyCode == 67) {
            if (document.selection) {
              oTextSelection = document.selection.createRange().text;
            } else {
              oTextSelection = window.getSelection().toString();
            }
            if (oTextSelection != '') {
              processSelection1();
              return false;
            }
          }
        };
      });
    </script>
  </head>
  <body class="bodyStyle">
    <div style="padding-left: 5px; height: 21px;position: relative;">
      <span>Syntaxes: </span>
      <input
        type="text"
        id="keyword"
        name="keyword"
        class="keywordselect"
        value=""
        autocomplete="off"
        onclick="hideKeyWord(false);"
        oninput="checkField(this.value)" /><span
        id="listStatus"
        class="showList"
        onclick="hideOrShowKeyWord();"
      ></span>
    </div>
    <div
      id="keywordInfo"
      class="keyInfoStyle"
      ><table
        id="keywordListInfo"
        cellspacing="0px"
        width="100%"
        style="font-size: 10px; line-height: 12px"
        ><tbody id="keywordInfoBody"></tbody></table
    ></div>
    <div id="divSuggestion" class="divSuggestion" style="padding: 10px 5px 5px 5px"
      ><p style="margin-bottom: 0px"
        >In the middle window, if you <span style="color: #ed6b44">choose the related SQL</span>,
        the SQL assistant will show the <span style="color: #ed6b44">related information</span>.</p
      ></div
    >
  </body>
</html>
